<?php


/**
 * Created by PhpStorm.
 * User: wanglin
 * Date: 2018/9/11
 * Time: 11:07
 */

namespace App\Console\Commands\ThirdOrder;


use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\BGAPIService;
use App\Services\UserService;
use common\enum\CommonEnum;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;



class BGOrderProcessor extends BaseOrderProcessor
{
    protected $signature = self::PREFIX . 'bg';
    protected $description = 'BG注单拉取';

    public $gameNames;
    public $bgService;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 60 * 60;
    // 自动重拉更新锁 redis key
    public $lockRedisKey = RedisKeys::BG_LOCK_AUTOFIX;
    // 拉取注单更新时间 redis key
    public $updateRedisKey = RedisKeys::BG_UPDATE_TIME;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;

    public static $BetStatusEnum = [
        1 => '未结算',
        2 => '结算赢',
        3 => '结算和',
        4 => '结算输',
        5 => '用户取消',
        6 => '过期',
        7 => '系统取消',
    ];


    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        $this->bgService = new BGAPIService();
        $this->partnerName = "BG";
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_LIVE;
        $this->reportOrderType = self::GAME_TYPE_LIVE;
        $this->userInfos = [];
    }


    /**
     * 向第三方API抓取数据
     */
    public function catchData()
    {
        // 检测是否需要重新拉取注单，每次重拉 1 小时前的注单
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        $startTime = strtotime($startTime);
        // 拉取 $this->timeInterval 的数据，减3秒是防止出现临界数据
        $this->latestUpdateKey = $endTime = min(time(), $startTime + $this->timeInterval-20*60);
        $data = $this->bgService->getBGOrders($startTime, $endTime);
        return $data;
    }


    /**
     * 处理数据
     */
    public function load($data)
    {
        $this->betTime = $data['bet_time'];
        $this->settleTime = $data['settle_time'];
        $this->reportTime = $data['report_time'];
        $this->betMoney = $data['bet_money'];
        $this->prizeMoney = $data['prize_money'];
        $this->winLossMoney = $data['winloss_money'];
        $this->apiUserName = $data['api_username'];
        $this->contributeMoney = $data['contribute_money'];
        $this->bonusMoney = $data['bonus_money'];
        $this->validMoney = $data['valid_money'];
        $this->commonStatus = $data['common_status'];
        $this->updateKey = $data['update_key'];
        $this->apiStatus = $data['api_status'];
        $this->gameCode = $data['game_code'];
        $this->gameName = $data['game_name'];
        $this->betContent = $data['bet_content'];
        $this->originData = $data['origin_data'];
        $this->orderNumber = $data["order_number"];
    }

    /**
     * 去除重复注单，UG，SB，BG 一次拉取会有重复注单出现，但是状态不变
     */
   public function removeRepeat($data)
    {
        $tmpRecord = [];
        $finalData = [];
        foreach ($data as $key => $value) {
            $orderNumber = $value['order_number'];
            if (isset($tmpRecord[$orderNumber])) {
                $index = $tmpRecord[$orderNumber];
                if ($finalData[$index]['api_status'] == 1) {
                    $finalData[$index] = $value;
                }
                continue;
            }
            $finalData[] = $value;
            $tmpRecord[$orderNumber] = count($finalData)-1;
        }
        return $finalData;
    }

    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    function initFromData($data)
    {
        // TODO: Implement initFromData() method.
    }


    function updateFlag($updateKey)
    {
        $redis=Redis::connection();
        $redis->set($this->updateRedisKey, date("Y-m-d H:i:s", $this->latestUpdateKey));
    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    function fixData($date)
    {
        // TODO: Implement fixData() method.
    }


}